package com.konjac.until;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Handle {

    /* 获取年月日时分秒 */
    public static String getDateAndTime() {
        String time=null;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());// 设置当前日期
        int year = calendar.get(Calendar.YEAR);// 获取年份
        int month = calendar.get(Calendar.MONTH) + 1;// 获取月
        int date = calendar.get(Calendar.DATE);// 获取日
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int min = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        time = year+"-"+month+"-"+date+"-"+hour+"-"+min+"-"+second;
        return time;
    }

    /* 提取String日期中的月份 */
    public static String getSaleMonth(String time) {
        String saleMonth=null;
        try {
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Date date =sdf.parse(time);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            saleMonth = (calendar.get(Calendar.MONTH)+1)+"";

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return saleMonth;
    }

    /* 将map根据value排序 */
    public static Map<String,Double> sortMap01(Map<String,Double> map) {
        //利用Map的entrySet方法，转化为list进行排序
        List<Map.Entry<String, Double>> entryList = new ArrayList<>(map.entrySet());
        //利用Collections的sort方法对list排序
        Collections.sort(entryList, new Comparator<Map.Entry<String, Double>>() {
            @Override
            public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {
                //正序排列，倒序反过来
                return (int)((o2.getValue() - o1.getValue())*100);
            }
        });
        //遍历排序好的list，一定要放进LinkedHashMap，因为只有LinkedHashMap是根据插入顺序进行存储
        LinkedHashMap<String, Double> linkedHashMap = new LinkedHashMap<String, Double>();
        for (Map.Entry<String,Double> e : entryList
        ) {
            linkedHashMap.put(e.getKey(),e.getValue());
        }
        return linkedHashMap;
    }

    /* 将map根据value排序2 */
    public static Map<String,Integer> sortMap02(Map<String,Integer> map) {
        //利用Map的entrySet方法，转化为list进行排序
        List<Map.Entry<String, Integer>> entryList = new ArrayList<>(map.entrySet());
        //利用Collections的sort方法对list排序
        Collections.sort(entryList, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                //正序排列，倒序反过来
                return (int)((o2.getValue() - o1.getValue())*100);
            }
        });
        //遍历排序好的list，一定要放进LinkedHashMap，因为只有LinkedHashMap是根据插入顺序进行存储
        LinkedHashMap<String, Integer> linkedHashMap = new LinkedHashMap<String, Integer>();
        for (Map.Entry<String,Integer> e : entryList
        ) {
            linkedHashMap.put(e.getKey(),e.getValue());
        }
        return linkedHashMap;
    }
}
